package com.lanou.horo.zhuizhui.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.horo.zhuizhui.R;
import com.lanou.horo.zhuizhui.utility.clickInterface.HomePageItemClickListener;
import com.lanou.horo.zhuizhui.utility.customImage.RoundNetWorkImageView;
import com.lanou.horo.zhuizhui.entity.PictureNewItem;
import com.lanou.horo.zhuizhui.utility.imageLoader.VolleySingleton;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/10/28.
 */
public class PictureContentAdapter extends
        RecyclerView.Adapter<PictureContentAdapter.PictureNewViewHolder> {
    private List<PictureNewItem.WelfaresEntity> entityList;
    private Context context;
    private HomePageItemClickListener itemClickListener;
    private ImageLoader imageLoaderVolley;
    private List<String> imgurlList = new ArrayList<>();

    public List<PictureNewItem.WelfaresEntity> getEntityList() {
        return entityList;
    }

    public void setEntityList(List<PictureNewItem.WelfaresEntity> entityList) {
        this.entityList = entityList;
    }

    public PictureContentAdapter(Context context) {
        this.context = context;
        imageLoaderVolley = VolleySingleton.getInstance().getImageLoader();
    }


    public void setItemClickListener(HomePageItemClickListener itemClick) {
        this.itemClickListener = itemClick;
    }


    public void addWelList(List<PictureNewItem.WelfaresEntity> datas) {
        entityList = datas;
        notifyDataSetChanged();
    }

    @Override
    public PictureNewViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.fragment_page_picture_new_item, null);
        return new PictureNewViewHolder(view);
    }

    @Override
    public void onBindViewHolder(PictureNewViewHolder holder, int position) {
        if (entityList != null && entityList.size() > 0) {
        holder.itemposition = position;
            PictureNewItem.WelfaresEntity entity = entityList.get(position);
            holder.pictureNewUserName.setText(entity.getUser_info().getName());
            holder.pictureNewTime.setText(entity.getCreated_at());
            if (entity.getIntroduce() != null) {
                holder.pictureNewTitle.setText(entity.getIntroduce());
            }
            String url = entity.getImg().getU();
            holder.pictureNewImg.setTag(url);
            holder.pictureNewImg.setImageUrl(url, imageLoaderVolley);


            if (holder.pictureNewImg.getDrawable() != null) {
                Bitmap bitmap = ((BitmapDrawable) holder.pictureNewImg.getDrawable()).getBitmap();
                //获取一张位图
                if (bitmap != null) {
                    //获取一个Builder
                    Palette.Builder from = Palette.from(bitmap);
                    //生成颜色 Builder.generate().getter方法 这里简单演示
                    int color = from.generate().getDarkVibrantColor(
                            context.getResources().getColor(android.R.color.white));
                    if (color != -1) {
                        String a = Integer.toHexString(-color);
                        if (a.length() > 1) {
                            int ab = Integer.parseInt(a.substring(0, 2), 16);
                            int cd = Integer.parseInt(a.substring(2, 4), 16);
                            int ef = Integer.parseInt(a.substring(4), 16);
                            if (ab < 200 || cd < 200 || ef < 200) {
                                holder.pictureNewUserName.setTextColor(0xffffffff);
                                holder.pictureNewTime.setTextColor(0xffffffff);
                                holder.pictureNewTitle.setTextColor(0xffffffff);
                            }
                        }
                    }
                    holder.cardView.setCardBackgroundColor(color);
                }
            }
            String urltx = entity.getUser_info().getAvatar();
            holder.pictureNewUserTx.setImageUrl(urltx, imageLoaderVolley);


            imgurlList.add(position,url);
        }

    }



    @Override
    public int getItemCount() {
        return entityList != null && entityList.size() > 0 ? entityList.size() : 0;
    }

    public class PictureNewViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private RoundNetWorkImageView pictureNewUserTx;
        private NetworkImageView pictureNewImg;
        private TextView pictureNewUserName, pictureNewTime, pictureNewTitle;
        private CardView cardView;
        private LinearLayout newLinearLayout;
        private int itemposition;


        public PictureNewViewHolder(View itemView) {
            super(itemView);
            newLinearLayout = (LinearLayout) itemView.findViewById(R.id.newLinearLayout);
            cardView = (CardView) itemView.findViewById(R.id.cardView);
            pictureNewImg = (NetworkImageView) itemView.findViewById(R.id.pictureNewImg);
            pictureNewUserTx = (RoundNetWorkImageView) itemView.findViewById(R.id.pictureNewUserTx);
            pictureNewUserName = (TextView) itemView.findViewById(R.id.pictureNewUserName);
            pictureNewTime = (TextView) itemView.findViewById(R.id.pictureNewTime);
            pictureNewTitle = (TextView) itemView.findViewById(R.id.pictureNewTitle);
            pictureNewImg.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (entityList != null) {
                PictureNewItem.WelfaresEntity w = entityList.get(itemposition);
                if (itemClickListener != null) {
                    itemClickListener.pictureItemClick(w, itemposition, imgurlList, pictureNewImg);
                }
            }
        }
    }
}
